// /rule/addRuleInfo.do
document.write('<script type="text/javascript" src="../static/js/back/utils.js"></script>');
document.write('<script type="text/javascript" src="../static/js/back/ajax.js"></script>');
var _url_ = '../goods/addGoodsInfo.do';
var _url_edit = '../order/updateOrderInfo.do';
var _url_list = '../order/getOrdersList';
var _url_id = '../goods/getGoodsById';
var _map = {};
///首页
var _pageNum = 1;
var _type ;
var spid="";
var dangqianorder;
  $(function(){
	   
	   spid = getCookie("shop_id");
	  _type = getUrlParam("type");
	  _type2 = getUrlParam("orderType");
	  console.log(_type2);
	  if(_type2){
		  _map['type']=_type2;
		  if(Number(_type2) == 2){
			  dangqianorder= getCookie('dangqianOrder');
			  console.log('当前桌台id:'+dangqianorder);
			  if(dangqianorder){
				  _map['userName'] = dangqianorder;
				  delCookie('dangqianOrder');
				  $('.alljiedan').show();
			  }
		  }
	  }
	  console.log(_type2);
	  console.log(_type);
	  if(_type === "add"){
		  getTypeListAdd();
		  $("#shopId").val(spid);
	  }else if(_type === "edit"){
		  _url_ = _url_edit;
//		  getTypeListAdd();
//		  $(".is_up_do").hide();
//		  showEditData();
	  }else{
		  //获取分类列表
//		  getTypeList();
		  getProfitsInfo();
		  getList();
	  }
  })
$(function(){
	  $("body").on("click",".mypage span",function(){
		  _pageNum = $(this).attr("data-status");
		  getList();
	  })
	  $(".search-btn").click(function(){
			 var tit =  $("#ser-title").val();
			 console.log(tit);
			 _map["orderName"] = tit;
			 $(".site-doc-icon").html('');
			 _pageNum = 1;
			 getList();
		  });
	  $("body").on("click",".left-type li",function(){
			 var tt = $(this).text();
			 $(this).addClass('lt-li-active').siblings().removeClass('lt-li-active');
			 _map['goodsTypeId'] = $(this).attr('data-status');
			 getList();
	  });
});
  function getList(){
	  _map['shopId'] = spid;
	  _map['pageNum'] = _pageNum;
	  _map["startTime"] = $("#startTime").val();
	  _map["endTime"] = $("#endTime").val();
//	  _map['pageSize'] = "1";
	  _ajax_get(_url_list,_map,function(res){
		  console.log(res);
		  if(!res.returnObject){
			  var hr = '<div style="    width: 100%; text-align: -webkit-center; color: #999; margin-top: 50px;">暂无数据</div>';
			  $(".site-doc-icon").html(hr);
			  return;
		  }
		  var html='',stat,statStr,payTypeStr,sendTypeStr;
		  var allMoney=0;
		  $.each(res.returnObject.list,function(i,v,arr){
//			  console.log(v);
			  if(v.status ==1 ){
				  stat=1;statStr = '待支付';
			  }else if(v.status ==2 ){
				  stat=2;statStr = '待接单';
			  }else if(v.status ==3 || v.status ==31 || v.status ==32 ){
				  if(_type2 == 2 ){
					  stat=v.status;statStr = '待上菜';
				  }else{
					  stat=v.status;statStr = '待送达';
				  }
			  }else if(v.status ==4 ){
				  stat=4;statStr = '已完成';
			  }else if(v.status ==5|| (v.status >50 && v.status < 59  ||v.status == 6) ){
				  stat=5;statStr = '已取消';
			  }
			  
			  if(v.sendType == 1){
				  sendTypeStr = '商家配送';
			  }
			  if(v.payType == 1){
				  payTypeStr = '微信支付';
			  }else{
				  payTypeStr = '现金结算';
			  }
			  
			  html+='<li><xblock class="order-xb"><span class="x-left">'
					+'  <span>#'+v.sort+'</span></span><span class="x-left x-two">';
			  		if(_type2==1){
			  			html+='  <span>'+v.sendTime+'</span>|<span>'+sendTypeStr+'</span>';
			  		}
//			  		console.log(stat);
			  		if(stat != 1 && stat != 5){
			  			html+='  <span class="print-btn" onclick="printXip(\''+v.id+'\')">打印小票</span>';
			  		}
					html+=' </span> <span class="x-right" style="line-height:40px;padding-right: 20px;">'+statStr+'</span>'
					+'  </xblock> <div class=" x-body order-info">'
					+'  <i class="title">'+v.shopTitle+'</i>';
					if(_type2==2){
						html+='<span> &nbsp;&nbsp;'+v.userAddress+'</span>';
					}
					html+='  <div class="order-btn">';
					if(stat == 2){
						html+='  <button type="button" onclick="member_no(this,\''+v.id+'\')" class="layui-btn layui-btn-primary">拒单</button>'
						+'  <button type="button" onclick="member_get(this,\''+v.id+'\')" class="layui-btn">接单</button>';
					}else if(stat == 3){
						if(_type2==2){
							html+='  <button type="button" data-status="32" data-st="'+v.status+'"   onclick="member_send(this,\''+v.id+'\')" class="layui-btn">确认菜已上齐</button>';
							html+='  <button type="button" data-status="31" data-st="'+v.status+'"  onclick="member_send(this,\''+v.id+'\')" class="layui-btn">确认已结账</button>';
						}else{
							html+='  <button type="button" data-status="4"  onclick="member_send(this,\''+v.id+'\')" class="layui-btn">确认送达</button>';
						}
					}else if(v.status == 32){
						html+='  <button type="button"  class="layui-btn layui-btn-primary">菜已上齐</button>';
						html+='  <button type="button" data-status="32" data-st="'+v.status+'"  onclick="member_send(this,\''+v.id+'\')" class="layui-btn">确认已结账</button>';
					}else if(v.status == 31){
						html+='  <button type="button" data-status="31" data-st="'+v.status+'"   onclick="member_send(this,\''+v.id+'\')" class="layui-btn">确认菜已上齐</button>';
						html+='  <button type="button" class="layui-btn layui-btn-primary">已结账</button>';
					}
					html+=' </div>';
//					console.log(_type2);
					if(_type2==1){
						console.log(_type2);
						html+=' <fieldset class="layui-elem-field layui-field-title" style="margin: 10px 0;"> </fieldset>'
							+'  <span>'+v.userName+'</span>'
							+'  <span>'+v.userPhone+'</span>'
							+'  <p>'+v.userAddress+'</p>';
					}
					html+='  <fieldset class="layui-elem-field layui-field-title" style="margin: 10px 0;"> </fieldset><p>商品信息</p> <span>备注:  </span>'
					+'  <span>'+v.mark+'</span>'
					+'  <fieldset class="layui-elem-field layui-field-title" style="margin: 10px 0;"> </fieldset>'
					+'  <div class="order-goods"> <table class="layui-table2"> <tbody>';
					var xiaoji = 0;
					for (var k = 0; k < v.list.length; k++) {
						var vv = v.list[k];
						html+='  <tr><td>'
								+vv.goodsTitle
								+'  </td><td>¥'
								+ (vv.goodsPrice / 100 ).toFixed(2)
								+'  </td><td>x'
								+vv.goodsNum
								+'  </td><td style="text-align:right;padding-right:20px">¥'
								+ ( (vv.goodsPrice * vv.goodsNum)/100).toFixed(2)
								+'  </td></tr> ';
						xiaoji += Number(vv.goodsPrice* vv.goodsNum );
					}
//					console.log(xiaoji);
					html+=' </tbody> </table>';
					if(_type2==1){
						html+='  <p>包装费 <span>'+(v.packPrice / 100).toFixed(2)+'</span></p>'
						+'  <p>配送费<span>'+(v.sendPrice / 100).toFixed(2)+'</span></p>';
						xiaoji+= Number( v.packPrice + v.sendPrice);
					}else{
						html+='  <p>餐具费 <span>'+(v.packPrice / 100).toFixed(2)+'</span></p>'
						xiaoji+= Number( v.packPrice);
					}
					
					html+='  <p><b>小计<span>¥'+(xiaoji/100).toFixed(2)+'</span></b></p> </div>'
					+'  <fieldset class="layui-elem-field layui-field-title" style="margin: 10px 0;"> </fieldset> <div class="order-goods">';
					if( v.activityIds){
						var huod = v.activityIds.split(',');
						for (var i = 0; i < huod.length; i++) {
							var hd = huod[i];
							if(hd.split('-')[0] == 1 ){
								html+='<p>新客立减<span>-'+(hd.split('-')[1] / 100).toFixed(2)+'元</span></p>';
							}else if(hd.split('-')[0] == 2){
								html+='  <p>满减活动 <span>-'+(hd.split('-')[1] / 100).toFixed(2)+'元</span></p>';
							}else if(hd.split('-')[0] == 3){
								html+='  <p>单品折扣 <span>-'+(hd.split('-')[1] / 100).toFixed(2)+'元</span></p>';
							}else if(hd.split('-')[0] == 4){
								html+='  <p>满减活动 <span>-'+(hd.split('-')[1] / 100).toFixed(2)+'元</span></p>';
							}else if(hd.split('-')[0] == 5){
								html+='  <p>堂食折扣 <span>-'+(hd.split('-')[1] / 100).toFixed(2)+'元</span></p>';
							}
						}
					}
					html+='  <p><b>支付方式</b><span>'+payTypeStr+'</span></p>'
					+'  </div> <fieldset class="layui-elem-field layui-field-title" style="margin: 10px 0;"> </fieldset> <div class="order-goods">'
					+'  <p><b>本单预计收入</b><span><b style="font-size:30px">¥'+(v.payPrice  / 100).toFixed(2) +'</b></span></p> </div>'
					+'  <p>本单顾客实际支付：<span>¥'+(v.payPrice  / 100).toFixed(2) +'</span><span class="x-red">('+statStr+')</span></p>'
					+'  <p>订单编号：<span>'+v.orderNo+'</span> 下单时间：<span >'+v.createTime+'</span>'
					+'  </p> </div> </li>';
					allMoney+=v.payPrice;
		  })
		  if(!res.returnObject.isLastPage){
			  $(".loadMore").html('<div onclick="loadMore()" style=" cursor:pointer;   width: 800px; text-align: -webkit-center; color: #999; margin-top: 50px;">加载更多...</div>');
		  }else{
			  $(".loadMore").html('<div style="  width: 800px; text-align: -webkit-center; color: #999; margin-top: 50px;">已经到底了</div>');
		  }
		  try {
			  $('.allMoney').text((allMoney/100)+"元");
		} catch (e) {
			// TODO: handle exception
		}
		  $(".site-doc-icon").append(html);
	  })
  }
  function alljiedan(){
	  console.log(dangqianorder);
	  $('.layui-btn').addClass('disabled');
	  _ajax_post_token('../order/updateOrderInfoTable.do',{tabId:dangqianorder},function(res){
		  console.log(res);
		  if(res.resultCode == 1000){
			  layer.msg('操作成功!',{icon:6,time:2000},function(){
				  $('.layui-btn').removeClass('disabled');
				  window.location.reload();
			  });
		  }else{
			  $('.layui-btn').removeClass('disabled');
			  layer.msg('操作失败!',{icon:6,time:2000});
		  }
	  })
  }
  function loadMore(){
	  _pageNum++;
	  getList();
  }
  function getProfitsInfo(){
	  _ajax_post_token('../order/selectOrderProfitToday',{shopId:spid,type:_type2},function(res){
		  console.log(res);
		  var list =  res.returnObject;
		  var profitCount = 0;
		  var totalCount = 0;
		  $.each(list,function(i,v,arr){
    		  console.log(v);
    		  if(v.typeId == 1){
    			  $(".today-profit p").eq(3).find("b").text((v.profit/100).toFixed(2));
    			  $(".today-profit p").eq(4).find("b").text(v.total);
    		  }else{
    			  $(".today-profit p").eq(5).find("b").text((v.profit/100).toFixed(2));
    			  $(".today-profit p").eq(6).find("b").text(v.total);
    		  }
    		  profitCount += Number(v.profit);
    		  totalCount += Number(v.total);
		  })
		  $(".today-profit p").eq(0).find("b").text((profitCount/100).toFixed(2));
		  $(".today-profit p").eq(1).find("b").text(totalCount);
	  })
  }
  
  function selectListByParam(d){
	  for(var key in d){
		  _map[key] =d[key];
		}
	  _pageNum = 1;
	  $(".site-doc-icon").html("");
	  getList();
  }
  
  function printXip(_id){
	  console.log(_id);
	  _ajax_post_token('../xorder/doPrinterOrder',{id:_id},function(res){
		  if(res.returnObject){
			  parent.setOrderInfo(res.returnObject);
		  }
	  });
  }
  function getTypeListAdd(){
	  _ajax_post_token('../goods/getGoodsTypeList',{pageSize:100},function(res){
		  console.log(res);
		  var list =  res.returnObject;
		  var html = "";
		  $.each(list.list,function(i,v,arr){
    		  if(list){
    			 html+='<option value="'+v.id+'">'+v.title+'</option>';
    		  }
		  })
		  $(".pIdsel").append(html);
	  })
  }
  function showEditData(){
	  _url_ = _url_edit;
	  var _id = getUrlParam("id");
	  $("#paramKey").val(_id);
	  console.log(_id);
	  _ajax_post_token(_url_id,{userType:1,id:_id},function(res){
		  console.log(res);
		 var ddt = res.returnObject;
		 ddt.priceOut = (ddt.priceOut/100).toFixed(2);
		 ddt.priceRoom = (ddt.priceRoom/100).toFixed(2);
		 ddt.goodsPackAmount = (ddt.goodsPackAmount/100).toFixed(2);
		  _showData = ddt;
	  });
  }    
      //
  /*用户-接单*/
  function member_get(obj,_id){
          //发异步删除数据
	  _url_ =_url_edit;
	  _ajax_post_token(_url_,{status:3,id:_id,shopId:spid},function(res){
	      	console.log(res);
	      	if(res.resultCode == 1000){
	      		 $(".site-doc-icon").html("");
	      		getList();
	      		layer.msg('接单成功!',{icon:6,time:2000});
	      	}else{
	      		layer.msg('接单失败!',{icon:5,time:2000});
	      	}
      });
  }
  /*用户-拒单*/
  function member_no(obj,_id){
	  //发异步删除数据
	  _url_ =_url_edit;
	  _ajax_post_token(_url_,{status:5,id:_id,shopId:spid},function(res){
		  console.log(res);
		  if(res.resultCode == 1000){
			  $(".site-doc-icon").html("");
			  getList();
			  layer.msg('订单已取消!',{icon:6,time:2000});
		  }else{
			  layer.msg('操作失败!',{icon:5,time:2000});
		  }
	  });
  }
  function member_send(obj,_id){
	  	  var stat = $(obj).attr("data-status");
	  	  var st = $(obj).attr("data-st");
	  	  if(st){
	  		  if(st==31||st==32){
	  			stat = 4;
	  		  }
	  	  }
		  _url_ =_url_edit;
		  _ajax_post_token(_url_,{status:stat,id:_id,shopId:spid},function(res){
			  console.log(res);
			  if(res.resultCode == 1000){
				  $(".site-doc-icon").html("");
				  getList();
				  layer.msg('操作成功!',{icon: 6,time:1000});
			  }else{
				  layer.msg('操作失败!',{icon:5,time:2000});
			  }
		  });
  }
  //批量上架
  function member_udall(){
	  var data = tableCheck.getData().join(',');
	  _ajax_post_token(_url_edit,{status:1,ids:data},function(res){
		  console.log(res);
		  if(res.resultCode == 1000){
			  $(".site-doc-icon").html("");
			  getList();
			  layer.msg('操作成功!',{icon: 6,time:1000});
		  }else{
			  layer.msg('操作失败!',{icon:5,time:2000});
		  }
	  });
  }
  function member_udall2(){
	  var data = tableCheck.getData().join(',');
	  _ajax_post_token(_url_edit,{status:2,ids:data},function(res){
		  console.log(res);
		  if(res.resultCode == 1000){
			  $(".site-doc-icon").html("");
			  getList();
			  layer.msg('操作成功!',{icon: 1,time:1000});
		  }else{
			  layer.msg('操作失败!',{icon:5,time:2000});
		  }
	  });
  }
  
  
  /*用户-启动*/
  function member_start(obj,_id){
	  layer.confirm('确认要解封此账号吗？',function(index){
		  //发异步删除数据
		  _url_ =_url_edit;
		  _ajax_post_token(_url_,{userStatus:1,id:_id},function(res){
			  console.log(res);
			  if(res.resultCode == 1000){
				  $(".site-doc-icon").html("");
				  getList();
				  layer.msg('已解封!',{icon: 6,time:1000});
			  }else if (res.resultCode == 1008){
				  layer.msg('当前账号已存在',{icon:5,time:2000});
			  }
			  else{
				  layer.msg('操作失败!',{icon:5,time:2000});
			  }
		  });
	  });
  }
      
  function updateInfo(data){
	  //发异步，把数据提交给后台
	 console.log(_url_);
	 data['shopId'] = spid;
      _ajax_post_token(_url_,data,function(res){
      	console.log(res);
      	if(res.resultCode == 1000){
      		$(".site-doc-icon").html("");
      	  parent.getList();
      	  parent.layer.msg('操作成功!',{icon:1,time:2000});
      	  x_admin_close();
          // 获得frame索引
      	}else{
      		layer.msg('操作失败!',{icon:1,time:2000});
      	}
      });
  }
      
 
      
//添加 和 修改窗口     